//
//  MPCardDetailViewController.m
//  MobileProject
//
//  Created by caiqilin on 2017/6/15.
//  Copyright © 2017年 caiqilin. All rights reserved.
//

#import "MPCardDetailViewController.h"
#import "MPCardDetailViewCell.h"
#import "MPCard.h"
#import "MJExtension.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "CategoryMeunModel.h"
#import "SVProgressHUD.h"
#import "MPWebViewController.h"
@interface MPCardDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong , nonatomic) UITableView *table;
@property (nonatomic, strong) NSMutableArray *cardCategoryArr;
@property (nonatomic, strong) NSMutableArray *cardArr;

@end

@implementation MPCardDetailViewController

- (NSMutableArray *)cardArr {
    if (!_cardArr) {
        _cardArr = [[NSMutableArray alloc] init];
    }
    return _cardArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
    
    [SVProgressHUD showWithStatus:@"正在加载中,请稍后"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self initData];
    });
}

- (void)initData{
    _cardCategoryArr=[NSMutableArray arrayWithCapacity:0];
    
    NSString *path=[[NSBundle mainBundle] pathForResource:@"CardCategory" ofType:@"plist"];
    NSArray *array=[NSArray arrayWithContentsOfFile:path];
    
    CategoryMeunModel * meun=[[CategoryMeunModel alloc] init];
    meun.menuImage=[array objectAtIndex:_index][@"menuImage"];
    self.navigationItem.title =[array objectAtIndex:_index][@"menuName"];
    meun.nextArray=[array objectAtIndex:_index][@"topMenu"];
    _cardArr = [MPCard mj_objectArrayWithKeyValuesArray:meun.nextArray];
    [SVProgressHUD dismiss];
    [self.table reloadData];
}

- (void)initView {
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.showsVerticalScrollIndicator = false;
    [self.table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.table.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.table];
    
    [self.table registerNib:[UINib nibWithNibName:@"MPCardDetailViewCell" bundle:nil] forCellReuseIdentifier:@"cardCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _cardArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MPCardDetailViewCell *orderManageCell =  [tableView dequeueReusableCellWithIdentifier:@"cardCell"];
    orderManageCell.card = _cardArr[indexPath.row];
    
    return orderManageCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:@"cardCell" configuration:^(MPCardDetailViewCell *cell) {
        cell.card = _cardArr[indexPath.row];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MPWebViewController *web = [[MPWebViewController alloc] init];
    web.menuTitle = @"xxx";
    [self.navigationController pushViewController:web animated:YES];
}

@end
